#include "ParticleGun.h"

#include "ParticleContext.h"
#include "DescriptorParticle.h"

#include "ParticleProjectile.h"

using namespace ParticleEngine;

const DescriptorParticle ParticleGun::Descriptor
(
"Gun",
[](ParticleContext& NewContext) { return new ParticleGun(NewContext); }
);

ParticleGun::ParticleGun(ParticleContext& NewContext) :
ParticleBase(NewContext, &ParticleGun::Descriptor),
PropertyAdhesions(0.15f),
Heat(1.0f),
Electricity(0.5f),
Counter(0)
{
	this->Adhesions = dynamic_cast<PropertyAdhesions*>(this);
	this->Adhesions->ParticleCast = dynamic_cast<ParticleBase*>(this);
}

ParticleGun::~ParticleGun()
{
}

void ParticleGun::Sole()//einzelne aktualisierzung
{
	this->SoleStandard();

	this->Heat.Sole();
	this->Electricity.Sole();

	this->Signal0.Sole();
	this->Signal1.Sole();

	Switch.Sole(this->Signal0, this->Signal1);
	if (Switch.Status == true)
		DirectionTracer.Sole(this);

	static float Electricity_Consum = 0.005f;
	static int Frequency = 200;
	static float Power = 0.1f;

	Counter++;
	if (Switch.Status == true && Electricity.Volume > Electricity_Consum && DirectionTracer.DirectorFound && (Counter % Frequency) == 0)
	{
		this->Electricity.Volume -= Electricity_Consum;

		ParticleBase *NewP;
		NewP = new ParticleProjectile(Context);
		NewP->Pos = this->Pos - DirectionTracer.Direction * 1.6f;
		NewP->Velocity = this->Velocity - DirectionTracer.Direction * Power;
		this->Velocity = this->Velocity + DirectionTracer.Direction * Power;
		this->Context.ToAddArray.Add(NewP);
	}
}
void ParticleGun::InteractionIndividual(ParticleBase*const Other, Vec<2, float>& Space, const float& DistanceSquared)
{
	this->InteractionKinetics(Other, Space, DistanceSquared);

	this->Heat.Interaction(Other->HasHeat());
	this->Electricity.Interaction(Other->HasElectricity());

	this->Signal0.Interaction(Other->HasSignal0());
	this->Signal1.Interaction(Other->HasSignal1());


}
ColorRGBA<unsigned char> ParticleGun::GetColor()
{
	return ColorRGBA<unsigned char>(0, 255, 255, 255);
}

void ParticleGun::SaveIndividual(std::ofstream& Strm)
{
	Heat.Save(Strm);
	Electricity.Save(Strm);

	Signal0.Save(Strm);
	Signal1.Save(Strm);
}
void ParticleGun::LoadIndividual(std::ifstream& Strm)
{
	Heat.Load(Strm);
	Electricity.Load(Strm);

	Signal0.Load(Strm);
	Signal1.Load(Strm);
}


